//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    isBindedTJ: app.globalData.isBindedTJ,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    page: 1,
    knowsize: 3,   //首页知识的数量
    recipesize:8,   //首页菜谱数量 
    // loginModal: false,
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    let token = wx.getStorageSync('storeToken')
    if (!token) return;

    app.auth.isBind();
  },
  onLoad: function () {
    var that = this;
    app.auth.isLogin(function () {
      app.globalData.userInfo = wx.getStorageSync('storeWxUser');
      that.setData({
        motto:'auth success',
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    })


    var urlBase = app.globalData.urlBase;
    var bannerImgBase = app.globalData.weixinResBase;
    this.setData({
      urlBase: urlBase,
      bannerBase: urlBase + bannerImgBase
    });
    //读取banner图片
    var getBannerImg = require("../../utils/getBannerImg");
    var bannerImgUrl = urlBase + "/wx/getWeixinResource?resType=1";
    getBannerImg(this, bannerImgUrl);

  
    //读取 top3 知识
    var page = this.data.page;
    wx.request({
      url: urlBase + '/wx/getKnowledgeInfos',
      method: 'get',
      data: {
        page: page,
        pagesize: this.data.knowsize
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          knowList: res.data
        });
        // console.log(res.data) 
      }
    });
    //读取top10菜谱
    wx.request({
      url: urlBase + '/wx/getRecipes',
      data: {
        page: page,
        pagesize: this.data.recipesize
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          recipeList: res.data
        });
        // console.log(res.data)
      }
    });
  },
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
  
})
